package org.example.mvc_start;

import ch.qos.logback.core.util.StringUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {
    @RequestMapping("/login")
    public Boolean login(String userName, String password, HttpSession session){
        if (!StringUtils.hasLength(userName)||!StringUtils.hasLength(password)){
            return false;
        }
        //进行用户名和密码的校验
        if ("admin".equals(userName) && "admin".equals(password)){
            //设置Session
            session.setAttribute("userName","admin");
            System.out.println("session设置成功");
            return true;
        }
        return false;
    }
    @RequestMapping("/getUserInfo")
    public String getUserInfo(HttpServletRequest request){
        //从Session 获取登录用户
        HttpSession session = request.getSession(false);
        String userName = null;
        if (session!=null) {
            userName = (String) session.getAttribute("userName");
            System.out.println(userName);
        }
        System.out.println(userName);
        return userName;
    }
}
